package com.tjsinfo.tjsoa.fileExpress.bo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.UUID;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.fileExpress.dao.IFileExpressGroupDAO;
import com.tjsinfo.tjsoa.fileExpress.vo.FileExpressFile;
import com.tjsinfo.tjsoa.fileExpress.vo.FileExpressGroup;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class FileExpressGroupBo implements IFileExpressGroupBo{
	private IQbcDao qbcDao;
	private IJDBCDao dao;
	private IFileExpressGroupDAO fileExpressGroupDAO;
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public IFileExpressGroupDAO getFileExpressGroupDAO() {
		return fileExpressGroupDAO;
	}
	public void setFileExpressGroupDAO(IFileExpressGroupDAO fileExpressGroupDAO) {
		this.fileExpressGroupDAO = fileExpressGroupDAO;
	}
	public IJDBCDao getDao() {
		return dao;
	}
	public void setDao(IJDBCDao dao) {
		this.dao = dao;
	}
	
	
	private static List	deleteList;
	public static List getDeleteList() {
		return deleteList;
	}
	
	public String[] getGroup(int user_id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressGroup.class);
		criteria.addOrder(Order.asc("createTime"));
		
		DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
		SimpleExpression b = Restrictions.eq("userId", user_id);
		criteria1.add(b);
		
		List<FileExpressGroup> list = qbcDao.getCriteria(criteria); 
		
		String[] strings = new String[list.size()*2];
		int i = 0;
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			FileExpressGroup group = (FileExpressGroup) iterator.next();
			strings[i] = group.getId().toString();
			i++;
			strings[i] = group.getGroupName();
			i++;
		}
		return strings;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#insertGroup(java.lang.String, int, java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_Group.IFileExpressGroupBo#insertGroup(java.lang.String, int)
	 */
	public String insertGroup(String groupName,int user_id) {
		SimpleExpression a = Restrictions.eq("userId", user_id);
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUser.class);
		criteria.add(a);
		
		FileExpressGroup group = new FileExpressGroup();
		group.setId(UUID.randomUUID().toString().replace("-", ""));
		group.setCreateTime(new Date());
		ArrayList<TSystemUser> list = (ArrayList)qbcDao.getCriteria(criteria);
		TSystemUser user = list.iterator().next();
		group.setTjsUser(user);
		
		group.setGroupName(groupName);
		
		String str = "error";
		try {
			fileExpressGroupDAO.save(group);
			str = "ok";
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	
	public String insertGroup(String uuid, String groupName,int user_id) {
		SimpleExpression a = Restrictions.eq("userId", user_id);
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUser.class);
		criteria.add(a);
		
		FileExpressGroup group = new FileExpressGroup();
		group.setId(uuid);
		group.setCreateTime(new Date());
		ArrayList<TSystemUser> list = (ArrayList)qbcDao.getCriteria(criteria);
		TSystemUser user = list.iterator().next();
		group.setTjsUser(user);
		
		group.setGroupName(groupName);
		
		String str = "error";
		try {
			fileExpressGroupDAO.save(group);
			str = "ok";
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#updateGroup(int, java.lang.String, int, java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_Group.IFileExpressGroupBo#updateGroup(int, java.lang.String, int)
	 */
	public String updateGroup(String id,String groupName,int user_id) {
		SimpleExpression a = Restrictions.eq("userId", user_id);
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUser.class);
		criteria.add(a);
		
		FileExpressGroup group = new FileExpressGroup();
		group.setId(id);
		ArrayList<TSystemUser> list = (ArrayList)qbcDao.getCriteria(criteria);
		TSystemUser user = list.iterator().next();
		group.setTjsUser(user);
		
		group.setGroupName(groupName);
		group.setCreateTime(new Date());
		
		String str = "error";
		try {
			fileExpressGroupDAO.update(group);
			str = "ok";
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#FirstDeleteGroup(int)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_Group.IFileExpressGroupBo#FirstDeleteGroup(int)
	 */
	public String FirstDeleteGroup(String groupid,String dirName) {
		SimpleExpression a = Restrictions.eq("id", groupid);
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressGroup.class);
		criteria.add(a);
		
		ArrayList<FileExpressGroup> list = (ArrayList)qbcDao.getCriteria(criteria);
		FileExpressGroup group = list.iterator().next();
		SimpleExpression b = Restrictions.eq("fileExpressGroup", group);
		DetachedCriteria criteria1 = DetachedCriteria.forClass(FileExpressFile.class);
		criteria1.add(b);
		ArrayList<FileExpressFile> list1 = (ArrayList)qbcDao.getCriteria(criteria1);
		String str = "ok";
		if(list1.size() != 0){
			str = "haveUser";
		}else {
			this.deleteGroup(groupid,dirName);
		}
		return str;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#deleteGroup(int)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_Group.IFileExpressGroupBo#deleteGroup(int)
	 */
	public String deleteGroup(String id,String dirName) {
		SimpleExpression a = Restrictions.eq("id", id);
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressGroup.class);
		criteria.add(a);
		
		ArrayList<FileExpressGroup> list = (ArrayList)qbcDao.getCriteria(criteria);
		FileExpressGroup group = list.iterator().next();
		
		SimpleExpression b = Restrictions.eq("fileExpressGroup", group);
		DetachedCriteria criteria1 = DetachedCriteria.forClass(FileExpressFile.class);
		criteria1.add(b);
		
		ArrayList<FileExpressFile> list1 = (ArrayList)qbcDao.getCriteria(criteria1);
		
		if (list1.size()>0) {
			dirName += "upload\\fileExpress\\"+ list1.iterator().next().getFileExpressGroup().getTjsUser().getUuid()+"\\";
			for (Iterator iterator = list1.iterator(); iterator.hasNext();) {
				FileExpressFile file = (FileExpressFile) iterator.next();
				String name = file.getFileName();
				String strdos = "cmd /c del \""+dirName+name+"\"";
	 		   	Process process;
				try {
					process = Runtime.getRuntime().exec(strdos);
					process.waitFor();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		String sql = "delete from t_fileExpress_group where id='"+id+"'";
		dao.executeUpdate(sql);
		dao.closeStmt();
		dao.closeConn();
		
		return "ok";
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#findById(int)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_Group.IFileExpressGroupBo#findById(int)
	 */
	public FileExpressGroup findById(String id) {
		FileExpressGroup group = fileExpressGroupDAO.findById(id);
		return group;
	}
	
	public FileExpressGroup findByMainGroup(int userid,String name) {
		FileExpressGroup group = new FileExpressGroup();
		
		SimpleExpression a = Restrictions.eq("groupName", name);
		SimpleExpression b = Restrictions.eq("userId", userid);
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressGroup.class);
		criteria.add(a);
		DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
		criteria1.add(b);
		ArrayList<FileExpressGroup> list = (ArrayList)qbcDao.getCriteria(criteria);
		String uuid = UUID.randomUUID().toString().replace("-", "");
		if (list.size()==0) {
			this.insertGroup(uuid, name, userid);
			group.setId(uuid);
		}else {
			group = list.iterator().next();
		}
		return group;
	}
}
